<!-- src/components/StudentDashboard.vue -->
<template>
  <div class="student-dashboard">
    <!-- 欢迎信息 -->
    <div class="welcome-section">
      <div class="welcome-content">
        <h2>欢迎回来，{{ studentName }}同学！</h2>
        <p>继续您的学习之旅，今天您有 {{ todayTasks }} 个学习任务待完成</p>
      </div>
      <div class="progress-ring">
        <div class="ring">
          <div class="progress" :style="{ '--progress': overallProgress }"></div><div class="progress-text">{{ overallProgress }}%</div>
        </div>
        <p class="progress-label">总体进度</p>
      </div>
    </div>

    <!-- 统计卡片 -->
    <div class="stats-grid">
      <div class="stat-card">
        <div class="stat-icon courses">
          <i class="icon-book"></i>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ statistics.enrolledCourses || 0 }}</div>
          <div class="stat-label">已报名课程</div>
          <div class="stat-change positive">+1 本月</div>
        </div>
      </div>
      
      <div class="stat-card">
        <div class="stat-icon completed">
          <i class="icon-check"></i>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ statistics.completedCourses || 0 }}</div>
          <div class="stat-label">已完成课程</div>
          <div class="stat-change positive">+2 本月</div>
        </div>
      </div>
      
      <div class="stat-card">
        <div class="stat-icon hours">
          <i class="icon-clock"></i>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ statistics.studyHours || 0 }}</div>
          <div class="stat-label">学习时长(小时)</div>
          <div class="stat-change positive">+12 本月</div>
        </div>
      </div>
      
      <div class="stat-card">
        <div class="stat-icon certificates">
          <i class="icon-award"></i>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ statistics.certificates || 0 }}</div>
          <div class="stat-label">获得证书</div>
          <div class="stat-change positive">+1 本月</div>
        </div>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="dashboard-content">
      <!-- 进行中的课程 -->
      <div class="content-card">
        <div class="card-header">
          <h3>进行中的课程</h3>
          <router-link to="/student/courses" class="view-all">查看全部</router-link>
        </div>
        <div class="ongoing-courses">
          <div v-if="ongoingCourses.length === 0" class="empty-state">
            <i class="icon-book-empty"></i>
            <p>暂无进行中的课程</p>
            <button class="btn-primary" @click="browseCourses">浏览课程</button>
          </div>
          <div v-else class="course-list">
            <div v-for="course in ongoingCourses" :key="course.id" class="course-item">
              <div class="course-image">
                <img :src="course.image" :alt="course.name" />
              </div>
              <div class="course-info">
                <h4>{{ course.name }}</h4>
                <p>{{ course.teacher }} · {{ course.nextClass }}</p>
                <div class="course-progress">
                  <div class="progress-bar">
                    <div class="progress-fill" :style="{ width: course.progress + '%' }"></div>
                  </div>
                  <span class="progress-text">{{ course.progress }}%</span>
                </div>
              </div>
              <div class="course-actions">
                <button class="btn-continue" @click="continueCourse(course.id)">继续学习</button>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 学习计划 -->
      <div class="content-card">
        <div class="card-header">
          <h3>今日学习计划</h3>
          <router-link to="/student/schedule" class="view-all">查看计划</router-link>
        </div>
        <div class="study-plan">
          <div v-if="todayPlan.length === 0" class="empty-state">
            <i class="icon-calendar-empty"></i>
            <p>今天暂无学习计划</p>
          </div>
          <div v-else class="plan-list">
            <div v-for="task in todayPlan" :key="task.id" class="plan-item">
              <div class="task-checkbox">
                <input type="checkbox" :checked="task.completed" @change="toggleTask(task.id)" />
              </div>
              <div class="task-info">
                <h4>{{ task.title }}</h4>
                <p>{{ task.course }} · {{ task.duration }}分钟</p>
              </div>
              <div class="task-time">
                <span>{{ task.time }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 学习统计 -->
    <div class="analytics-section">
      <div class="content-card full-width">
        <div class="card-header">
          <h3>学习统计</h3>
          <div class="chart-controls">
            <select v-model="chartPeriod" @change="updateChart">
              <option value="week">本周</option>
              <option value="month">本月</option>
              <option value="quarter">本季度</option>
            </select>
          </div>
        </div>
        <div class="chart-container">
          <div class="chart-placeholder">
            <p>学习数据图表区域</p>
            <p>可显示：学习时长、课程进度、测试成绩等</p>
          </div>
        </div>
      </div>
    </div>

    <!-- 最新通知 -->
    <div class="notifications-section">
      <div class="content-card">
        <div class="card-header">
          <h3>最新通知</h3>
          <router-link to="/student/notifications" class="view-all">查看全部</router-link>
        </div>
        <div class="notification-list">
          <div v-for="notification in recentNotifications" :key="notification.id" class="notification-item">
            <div class="notification-icon">
              <i :class="getNotificationIcon(notification.type)"></i>
            </div>
            <div class="notification-content">
              <h4>{{ notification.title }}</h4>
              <p>{{ notification.content }}</p>
              <span class="notification-time">{{ notification.time }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'

const router = useRouter()
const authStore = useAuthStore()

// 响应式数据
const statistics = ref({
  enrolledCourses: 0,
  completedCourses: 0,
  studyHours: 0,
  certificates: 0
})

const ongoingCourses = ref([
  {
    id: 1,
    name: '农业种植技术基础',
    teacher: '张老师',
    nextClass: '明天 14:00',
    progress: 65,
    image: '/course-placeholder.jpg'
  },
  {
    id: 2,
    name: '现代农机操作',
    teacher: '李老师',
    nextClass: '后天 09:00',
    progress: 40,
    image: '/course-placeholder.jpg'
  }
])

const todayPlan = ref([
  {
    id: 1,
    title: '观看种植技术视频',
    course: '农业种植技术基础',
    duration: 30,
    time: '09:00',
    completed: false
  },
  {
    id: 2,
    title: '完成章节测试',
    course: '农业种植技术基础',
    duration: 15,
    time: '14:00',
    completed: true
  }
])

const recentNotifications = ref([
  {
    id: 1,
    type: 'course',
    title: '新课程上线',
    content: '《智能农业管理系统》课程已上线，快来报名吧！',
    time: '2小时前'
  },
  {
    id: 2,
    type: 'assignment',
    title: '作业提醒',
    content: '《农业种植技术基础》课程作业即将截止',
    time: '1天前'
  }
])

const chartPeriod = ref('week')
const overallProgress = ref(68)

// 计算属性
const studentName = computed(() => {
  return authStore.userInfo?.name || '同学'
})

const todayTasks = computed(() => {
  return todayPlan.value.filter(task => !task.completed).length
})

// 方法
const browseCourses = () => {
  router.push('/student/courses?tab=available')
}

const continueCourse = (courseId) => {
  router.push(`/student/courses/${courseId}`)
}

const toggleTask = (taskId) => {
  const task = todayPlan.value.find(t => t.id === taskId)
  if (task) {
    task.completed = !task.completed
  }
}

const getNotificationIcon = (type) => {
  const iconMap = {
    course: 'icon-book',
    assignment: 'icon-clipboard',
    system: 'icon-bell',
    achievement: 'icon-award'
  }
  return iconMap[type] || 'icon-bell'
}

const updateChart = () => {
  console.log('更新图表:', chartPeriod.value)
}

const fetchStudentData = async () => {
  try {
    // 这里调用API获取学生相关数据
    console.log('获取学生数据')
  } catch (error) {
    console.error('获取学生数据失败:', error)
  }
}

onMounted(() => {
  fetchStudentData()
})
</script>

<style scoped>
.student-dashboard {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.welcome-section {
  background: linear-gradient(135deg, #3B82F6 0%, #1D4ED8 100%);
  color: white;
  padding: 24px;
  border-radius: 12px;
  margin-bottom: 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.welcome-content h2 {
  margin: 0 0 8px 0;
  font-size: 24px;
}

.welcome-content p {
  margin: 0;
  opacity: 0.9;
}

.progress-ring {
  text-align: center;
}

.ring {
  position: relative;
  width: 80px;
  height: 80px;
  margin: 0 auto 8px;
}

.progress {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: conic-gradient(
    white calc(var(--progress) * 1%),
    rgba(255, 255, 255, 0.3) 0
  );
  display: flex;
  align-items: center;
  justify-content: center;
}

.progress-text {
  font-size: 18px;
  font-weight: 600;
}

.progress-label {
  margin: 0;
  font-size: 14px;
  opacity: 0.9;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
  margin-bottom: 24px;
}

.stat-card {
  background: white;
  padding: 24px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  display: flex;
  align-items: center;
  gap: 16px;
}

.stat-icon {
  width: 56px;
  height: 56px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 24px;
}

.stat-icon.courses { background: #3B82F6; }
.stat-icon.completed { background: #10B981; }
.stat-icon.hours { background: #F59E0B; }
.stat-icon.certificates { background: #EF4444; }

.stat-number {
  font-size: 28px;
  font-weight: bold;
  color: #1F2937;
  margin-bottom: 4px;
}

.stat-label {
  color: #6B7280;
  font-size: 14px;
  margin-bottom: 4px;
}

.stat-change {
  font-size: 12px;
  font-weight: 500;
}

.stat-change.positive {
  color: #10B981;
}

.icon-check::before {
  content: "✅";
}

.icon-clock::before {
  content: "⏰";
}

.dashboard-content {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  margin-bottom: 24px;
}

.content-card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  overflow: hidden;
}

.content-card.full-width {
  grid-column: 1 / -1;
}

.card-header {
  padding: 20px 24px 16px;
  border-bottom: 1px solid #E5E7EB;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header h3 {
  margin: 0;
  color: #1F2937;
  font-size: 18px;
}

.view-all {
  color: #3B82F6;
  text-decoration: none;
  font-size: 14px;
  font-weight: 500;
}

.view-all:hover {
  text-decoration: underline;
}

.ongoing-courses,
.study-plan {
  padding: 0 24px 24px;
}

.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: #6B7280;
}

.empty-state i {
  font-size: 48px;
  margin-bottom: 16px;
  display: block;
}

.icon-book-empty::before {
  content: "📚";
}

.btn-primary {
  background: #3B82F6;
  color: white;
  border: none;
  padding: 12px 24px;
  border-radius: 8px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s;
  margin-top: 16px;
}

.btn-primary:hover {
  background: #2563EB;
}

.course-list,
.plan-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.course-item {
  display: flex;
  gap: 16px;
  padding: 16px;
  background: #F9FAFB;
  border-radius: 8px;
}

.course-image {
  width: 80px;
  height: 60px;
  border-radius: 6px;
  overflow: hidden;
}

.course-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: #E5E7EB;
}

.course-info {
  flex: 1;
}

.course-info h4 {
  margin: 0 0 4px 0;
  color: #1F2937;
  font-size: 16px;
}

.course-info p {
  margin: 0 0 8px 0;
  color: #6B7280;
  font-size: 14px;
}

.course-progress {
  display: flex;
  align-items: center;
  gap: 8px;
}

.progress-bar {
  flex: 1;
  height: 6px;
  background: #E5E7EB;
  border-radius: 3px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: #3B82F6;
  transition: width 0.3s;
}

.progress-text {
  font-size: 12px;
  color: #6B7280;
  min-width: 35px;
}

.course-actions {
  display: flex;
  align-items: center;
}

.btn-continue {
  background: #3B82F6;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.2s;
}

.btn-continue:hover {
  background: #2563EB;
}

.plan-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  background: #F9FAFB;
  border-radius: 8px;
}

.task-checkbox input {
  width: 18px;
  height: 18px;
  cursor: pointer;
}

.task-info {
  flex: 1;
}

.task-info h4 {
  margin: 0 0 2px 0;
  color: #1F2937;
  font-size: 14px;
}

.task-info p {
  margin: 0;
  color: #6B7280;
  font-size: 12px;
}

.task-time {
  color: #6B7280;
  font-size: 14px;
  font-weight: 500;
}

.analytics-section,
.notifications-section {
  margin-bottom: 24px;
}

.chart-controls {
  display: flex;
  gap: 12px;
}

.chart-controls select {
  padding: 8px 12px;
  border: 1px solid #D1D5DB;
  border-radius: 6px;
  font-size: 14px;
}

.chart-container {
  padding: 24px;
}

.chart-placeholder {
  height: 300px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #F9FAFB;
  border-radius: 8px;
  color: #6B7280;
  text-align: center;
}

.notification-list {
  padding: 0 24px 24px;
}

.notification-item {
  display: flex;
  gap: 12px;
  padding: 16px 0;
  border-bottom: 1px solid #F3F4F6;
}

.notification-item:last-child {
  border-bottom: none;
}

.notification-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #F3F4F6;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #6B7280;
}

.notification-content {
  flex: 1;
}

.notification-content h4 {
  margin: 0 0 4px 0;
  color: #1F2937;
  font-size: 14px;
}

.notification-content p {
  margin: 0 0 4px 0;
  color: #6B7280;
  font-size: 13px;
}

.notification-time {
  color: #9CA3AF;
  font-size: 12px;
}

.icon-clipboard::before {
  content: "📋";
}

@media (max-width: 768px) {
  .welcome-section {
    flex-direction: column;
    text-align: center;
    gap: 16px;
  }
  
  .dashboard-content {
    grid-template-columns: 1fr;
  }
  
  .stats-grid {
    grid-template-columns: 1fr;
  }
  
  .course-item {
    flex-direction: column;
  }
  
  .course-image {
    width: 100%;
    height: 120px;
  }
}
</style>